# -*- coding: iso-8859-2 -*-

from authorshipanalyzer.authorslot import AuthorSlot

from authorshipanalyzer.authorslot.filler import CharacterFeaturesFiller
from authorshipanalyzer.authorslot.filler import DocumentFeaturesFiller
from authorshipanalyzer.authorslot.filler import WordFeaturesFiller
from authorshipanalyzer.authorslot.filler import ProfileFeaturesFiller
from authorshipanalyzer.authorslot.filler import ErrorFeaturesFiller
from authorshipanalyzer.authorslot.filler import EmailFeaturesFiller

from CategoryFiller import CategoryFiller

class AuthorSlotManager:
    '''
    Class responsible for message slots management. It compose them
    into author slot. It also function as repository.
    @oaram authorSlots: dictionary<String::author, AuthorSlot>
    '''
    
    def __init__(self):
        self.authorSlots = {}
        self.fillers = []
        self.fillers.append(CharacterFeaturesFiller())
        self.fillers.append(DocumentFeaturesFiller())
        self.fillers.append(WordFeaturesFiller())
        self.fillers.append(ProfileFeaturesFiller())
        self.fillers.append(ErrorFeaturesFiller())
        self.fillers.append(EmailFeaturesFiller())
        self.categoryFiller = CategoryFiller()

    def getAuthorSlots(self):
        return self.authorSlots

    
    def addSlot(self, slot):
        '''
        Add slot to the proper author slot
        '''
        author = slot.getAuthor()
        if not self.authorSlots.has_key(author):
            self.authorSlots[author] = AuthorSlot()
        self.authorSlots[author].addSlot(slot)
        
    def addSlots(self, slots):
        '''
        @param slots: list of slots
        '''
        
        for slot in slots:
            self.addSlot(slot)
    
    def removeAuthor(self, author):
        if self.authorSlots.has_key(author):
            self.authorSlots.pop(author)
    
    def recompute(self):
        '''
        Recompute slots
        '''    
        for authorSlot in self.authorSlots.values():
            print "Creating slot for %s..." % authorSlot.author
            for filler in self.fillers:
                filler.fill(authorSlot)
        
        self.categoryFiller.fill(self.authorSlots)
                
        
    def getAuthorSlot(self, author):
        return self.authorSlots[author]
    
    def getAuthors(self):
        return self.authorSlots.keys()

    authorSlots = property(getAuthorSlots, None, None, None)
    
if __name__ == "__main__":
    import datetime
    from time import time
    import os
    
    from authorshipanalyzer.message import Message
    from authorshipanalyzer.message import MessageReader
    from authorshipanalyzer.file import Path
    from authorshipanalyzer.slot import SlotManager
    from authorshipanalyzer.slot import Slot
    from authorshipanalyzer.slot.filler import SexFiller

    fromDate = datetime.date(2008, 10, 1)
    toDate = datetime.date(2008, 10, 3)
    
    mr = MessageReader(Path.getMainDir() + os.sep + "info1")
    messages = mr.readMessages(fromDate, toDate)
               
    asm = AuthorSlotManager()
    sm = SlotManager()
    
    start = time()
    print "Create slots..."
    sm.createSlots(messages)
    print "Slots created!\nCreate author slots..."
    asm.addSlots(sm.getSlots())
    print "Author slots created!"
    end = time()    
    print "Generated in: "  + str(end - start) + " seconds"  
    
    start = time()
    print "Create author slots"
    asm.recompute()
    end = time()    
    print "Generated in: "  + str(end - start) + " seconds" 
    
    for author, slot in asm.authorSlots.items():
        print author + " " + str(len(slot.slots))
        print "Slot:"
        print slot.cAlphabetic
        print slot.cUpperCase
        print slot.cDigit
        print slot.cWhiteSpace
        print slot.cSpace
        print slot.cSpacePerWhite
        print slot.cTab
        print slot.cTabPerWhite
        print slot.cPunctuation
        
        print slot.wAvgWordLength
        print slot.wVocabularyRichness
        print "Function words: " + str(slot.wFunctionWord)
        print slot.wShortWords
        print slot.wHapaxLegomentaN
        print slot.wHapaxLegomentaV
        
        print slot.dBlankLines
        print slot.dSentences
        
        print slot.eOrthographic
        print slot.eShift
        print slot.eKeyboard
        print slot.eOther
        
        print slot.categories
        
        print slot.age
        print slot.status
        print slot.sex
        
        print "--------------------------++++++++++++"
    
#    from authorshipanalyzer.analyze import FooterAnalyzer
#    
#    fa = FooterAnalyzer()
#    
#    for author in asm.getAuthors():
#        print author, " ", 
#        print len(asm.getAuthorSlot(author).getMessages())
#        fa.analyzeMessages(asm.getAuthorSlot(author).getMessages())
#        for message in asm.getAuthorSlot(author).getMessages():
#            print "MESSAGE:\n" + message.getText()
#            print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
#            print "SIG:\n" + message.getSignature()
#        print "-------------------------"
#    end = time()
#    
#    print "Generated in: "  + str(end - start) + " seconds"  
#    recognizedSex = 0.0
#    man = 0.0
#    woman = 0.0
#    for author in asm.getAuthors():
#        sex = asm.getAuthorSlot(author).getSex()
#        if sex != "unknown":
#            recognizedSex += 1.0
#            if sex == "woman":
#                woman += 1.0
#            if sex == "man":
#                man += 1.0
#        print asm.getAuthorSlot(author).getAuthor(), asm.getAuthorSlot(author).getSex(), \
#            len(asm.getAuthorSlot(author).getSlots())
#            
#    print man, woman, recognizedSex
#    print "Number of processed slots:", len(asm.getAuthors())        
#    print "Sex recognized in " + str(recognizedSex / len(asm.getAuthors()) * 100) + "%"
#    print "Woman: " + str(woman / len(asm.getAuthors()) * 100) + "%"
#    print "Man: " + str(man / len(asm.getAuthors()) * 100) + "%"
            
